<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form size="small" :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="专家组" prop="expertGroupId">
      <el-input :readonly="true" v-model="dataForm.expertGroupName" placeholder="专家组"></el-input>
    </el-form-item>
    <el-form-item label="标段" prop="segmentId" style="margin-top: 20px">
      <el-input v-model="dataForm.segmentId" placeholder="选择标段"></el-input>
    </el-form-item>
    <el-form-item label="备注" style="margin-top: 20px">
      <el-input type="textarea" v-model="dataForm.remark" placeholder="备注"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          expertGroupId: '',
          expertGroupName: '',
          segmentId: '',
          segmentName: '',
          remark: '',
          createOn: '',
          createUserId: '',
          createBy: '',
          modifiedOn: '',
          modifiedUserId: '',
          modifiedBy: ''
        },
        dataRule: {
          expertGroupId: [
            { required: true, message: '专家组ID不能为空', trigger: 'blur' }
          ],
          segmentId: [
            { required: true, message: '标段ID不能为空', trigger: 'blur' }
          ],
          remark: [
            { required: true, message: '备注不能为空', trigger: 'blur' }
          ],
          createOn: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          createUserId: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          createBy: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          modifiedOn: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          modifiedUserId: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          modifiedBy: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id, groupId, groupName) {
        this.dataForm.id = id || 0
        this.dataForm.expertGroupId = groupId || 0
        this.dataForm.expertGroupName = groupName || ''
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/expert/expertgroupsegment/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.expertGroupId = data.expertGroupSegment.expertGroupId
                this.dataForm.segmentId = data.expertGroupSegment.segmentId
                this.dataForm.remark = data.expertGroupSegment.remark
                this.dataForm.createOn = data.expertGroupSegment.createOn
                this.dataForm.createUserId = data.expertGroupSegment.createUserId
                this.dataForm.createBy = data.expertGroupSegment.createBy
                this.dataForm.modifiedOn = data.expertGroupSegment.modifiedOn
                this.dataForm.modifiedUserId = data.expertGroupSegment.modifiedUserId
                this.dataForm.modifiedBy = data.expertGroupSegment.modifiedBy
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/expert/expertgroupsegment/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'expertGroupId': this.dataForm.expertGroupId,
                'segmentId': this.dataForm.segmentId,
                'remark': this.dataForm.remark,
                'createOn': this.dataForm.createOn,
                'createUserId': this.dataForm.createUserId,
                'createBy': this.dataForm.createBy,
                'modifiedOn': this.dataForm.modifiedOn,
                'modifiedUserId': this.dataForm.modifiedUserId,
                'modifiedBy': this.dataForm.modifiedBy
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
